const {ipcMain} = require('electron')
const { db,query,insertAsync,update } = require('./db.js');
exports.ipcMainBind = function(){

    ipcMain.on('insert', async (event, arg) => {
        console.log('insert='+arg);
        //insert( arg );
        let res = await insertAsync( arg );
        //event.returnValue = insert(arg);
        //event.reply('insert', 'insert success');
        event.sender.send('insert', res);
    });

    ipcMain.on('query', async (event, arg) => {
        console.log('query='+arg);
        let res = await query( arg );
        //event.reply('query', 'query success');
        event.sender.send('query', res );
       // event.returnValue = 'query success';
    });
    ///要在ready 之后 才能使用  
    //同步等待
    ipcMain.handle('query2', async (event, ...args) => {
        let res = await query( args[0] );
        return res; // 将结果返回给渲染进程
    });
    ipcMain.on('update', async (event, arg) => {
        console.log('update='+arg);
        let res = await update( arg );
        //event.reply('update', 'update success');
        event.sender.send('update', res );
       // event.returnValue = 'update success';
    });

}